package com.jwthhealth_pub.common.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.View;

import com.jwthhealth_pub.R;

/**
 * 轮播图底部弧线
 * Created by liuzhen on 2017/12/5.
 */

public class JwthBannerBottom extends View {

    private Paint mPaint, mPointPaint;
    private Path mArcPath;
    private int mWid;
    private int mHei;

    private int mSelectPos;

    private int mPointSize;

    public void setmSelectPos(int mSelectPos) {
        this.mSelectPos = mSelectPos;
        invalidate();
    }

    public void setmPointSize(int mPointSize) {
        this.mPointSize = mPointSize;
    }

    public JwthBannerBottom(Context context, AttributeSet attrs) {
        super(context, attrs);

        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.JwthBannerBottom);

        mPointSize = ta.getIndex(R.styleable.JwthBannerBottom_point_size);

        ta.recycle();

        init();
    }

    private void init() {
        mPaint = new Paint();
        mPointPaint = new Paint();
        mArcPath = new Path();

        mSelectPos = 1;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        mWid = getWidth();
        mHei = getHeight();

        drawBottomArc(canvas);
        drawIndicatPoints(canvas);
    }

    private void drawIndicatPoints(Canvas canvas) {
        if (mPointSize == 0) return;

        mPointPaint.setStyle(Paint.Style.FILL);
        mPointPaint.setStrokeWidth(1);
        mPointPaint.setAntiAlias(true);

        int indicatorSpace = 35;
        int indicatorsStartPos = (mWid - (mPointSize - 1) * indicatorSpace) / 2;

        for (int i=0; i<mPointSize; i++) {
            if (i+1 == mSelectPos || i+1 == mSelectPos - mPointSize)
                mPointPaint.setColor(Color.WHITE);
            else
                mPointPaint.setColor(getResources().getColor(R.color.dividualGray));

            canvas.drawCircle(indicatorsStartPos + i*indicatorSpace, mHei - 80, 7, mPointPaint);
        }
    }

    private void drawBottomArc(Canvas canvas) {

        mPaint.setColor(Color.parseColor("#FAFAFA"));
        mPaint.setAntiAlias(true);
        mPaint.setStrokeWidth(2);
        mPaint.setStyle(Paint.Style.FILL);//设置填充path内部区域

        mArcPath.reset();
        mArcPath.moveTo(0, mHei);
        //绘制贝塞尔曲线 quadTo(control.x, control.y, end.x, end.y)
        mArcPath.quadTo((float) (mWid / 2), (float) (mHei - 90), mWid, mHei);
        mArcPath.lineTo(0, mHei);

        canvas.drawPath(mArcPath, mPaint);
    }
}
